﻿var OrderMgr = {
    orderListPix: "sv.order.list.",
    orderListLeftNumPix: "sv.order.list.left.",
    orderListCurIndex: "sv.order.list.cur.index",
    orderInfoCur: "sv.order.cur.info",
    queryOrderNum: 10,
    allOrderListType: 0,
    failPayOrderListType: 1,
    unpickedOrderListType: 2,
    pickedOrderListType: 3,
    failPayOrderType: 1,
    unpickedOrderType: 2,
    pickedOrderType: 3,
    zhifuPayType: 1,
    weixinPayType: 2,
    jifenPayType: 3,
    codPaytType: 4,
    orderCancelOp: 0,
    orderPayOp: 1,
    orderTakeOp: 2,
    codePick: 1,
    akeyPick: 2,
    orderChangeNotifyCallback: null,
    orderGoodsChangeNotifyCallback: null,
    pickedGoodsSucNotifyCallback: null,
    pickedGoodsErrorNotifyCallback: null,
    setOrderChangeNotifyCallback: function (callback) {
        OrderMgr.orderChangeNotifyCallback = callback;
    },
    resetOrderChangeNotifyCallback: function () {
        OrderMgr.orderChangeNotifyCallback = null;
    },
    setOrderGoodsChangeNotifyCallback: function(callback) {
        OrderMgr.orderGoodsChangeNotifyCallback = callback;
    },
    resetOrderGoodsChangeNotifyCallback: function() {
        OrderMgr.orderGoodsChangeNotifyCallback = null;
    },
    setPickedGoodsSucNotifyCallback: function (callback) {
        OrderMgr.pickedGoodsSucNotifyCallback = callback;
    },
    resetPickedGoodsSucNotifyCallback: function() {
        OrderMgr.pickedGoodsSucNotifyCallback = null;
    },
    setPickedGoodsErrorNotifyCallback: function(callback) {
        OrderMgr.pickedGoodsErrorNotifyCallback = callback;
    },
    resetPickedGoodsErrorNotifyCallback: function() {
        OrderMgr.pickedGoodsErrorNotifyCallback = null;
    },
    setCurOrderInfo: function(orderInfo) {
        setStorage(OrderMgr.orderInfoCur, orderInfo);
    },
    getCurOrderInfo: function() {
        return getStorageVal(OrderMgr.orderInfoCur);
    },
    //akey_pick点击取货按钮的函数。pickingAnim表示中间的动画
    aKeyPickGoods: function(goodsId, goodsCode) {
        var curOrderInfo = OrderMgr.getCurOrderInfo();
        if (null != curOrderInfo) {
            var sessionId = UserInfoMgr.getSessionId();
            var aKeyPickRequestJson = {
                "order_id": curOrderInfo.orderid,
                "good_id": goodsId,
                "point_id": curOrderInfo.point_info.id,
                "goods_code": goodsCode,
                "session_id": sessionId
            }

            doAjax(aKeyPickRequestJson, "akey_pick", function (msg) {
                if (msg != null && msg != "") {
                    setTimeout(function (e) { OrderMgr.getAkeyPickResult(e) }, 8000, aKeyPickRequestJson);
                }
            });
        }
    },
    setOrderListCurIndex: function(type) {
        setStorage(OrderMgr.orderListCurIndex, type);
    },
    getOrderListCurIndex: function() {
        var curIndex = OrderMgr.allOrderListType;
        var curIndexVal = getStorageVal(OrderMgr.orderListCurIndex);
        if (null == curIndexVal) {
            var unPickNum = UserInfoMgr.getUnpickNum();
            if (unPickNum > 0) {
                curIndex = OrderMgr.unpickedOrderListType;//默认显示未送货列表
            }
        } else {
            curIndex = Number(curIndexVal);
        }
        return curIndex;
    },
    getCurOrderList: function() {
        var curIndex = OrderMgr.getOrderListCurIndex();
        return getStorageVal(OrderMgr.orderListPix + curIndex);
    },
    isOrderListLeft: function(type) {
        var hasLeft = true;
        var leftNumVal = getStorageVal(OrderMgr.orderListLeftNumPix + type);
        if ((null != leftNumVal) && (0 == Number(leftNumVal))) {
            hasLeft = false;
        }
        return hasLeft;
    },
    getOrderList: function (type, from, num, getOrderListCallback) {
        var orderList = getStorageVal(OrderMgr.orderListPix + type);
        if (null != orderList) {
            var size = orderList.length;
            if (size > (from + num)) {
                if (getOrderListCallback) {
                    getOrderListCallback(orderList);
                }
            } else {
                OrderMgr.tryGetOrderListFromService(type, size, Number(OrderMgr.queryOrderNum), getOrderListCallback);
            }
        } else {
            OrderMgr.tryGetOrderListFromService(type, from, num, getOrderListCallback);
        }
    },
    tryGetOrderListFromService: function (type, from, num, getOrderListCallback) {
        var hasLeft = OrderMgr.isOrderListLeft(type);
        if (true == hasLeft) {
            OrderMgr.getOrderListFromService(type, from, num, getOrderListCallback);
        } else {
            if (getOrderListCallback) {
                var orderList = getStorageVal(OrderMgr.orderListPix + type);
                getOrderListCallback(orderList);
            }
        }
    },
    getOrderListRequestJson: function (type, from, num) {
        var sessionId = UserInfoMgr.getSessionId();
        var orderListRequestJson = {
            "from": Number(from),
            "number": Number(num),
            "orderlist_type": Number(type),
            "session_id": sessionId
        }
        return orderListRequestJson;
    },
    getOrderListFromService: function (type, from, num, getOrderListCallback) {
        var orderListRequestJson = OrderMgr.getOrderListRequestJson(type, from, num);
        doAjax(orderListRequestJson, "get_order_list", function (msg) {
            if (msg.result == "0") {
                OrderMgr.addOrderList(msg.orderlist_type, msg.order_info, msg.left_num);
                if (null != getOrderListCallback) {
                    var curOrderListIndex = OrderMgr.getOrderListCurIndex();
                    if (curOrderListIndex == Number(msg.orderlist_type)) {
                        var orderList = getStorageVal(OrderMgr.orderListPix + curOrderListIndex);
                        getOrderListCallback(orderList);
                    }
                }
            }
        });
    },
    addOrderList: function (type, orderList, leftNum) {
        setStorage(OrderMgr.orderListLeftNumPix + type, leftNum);
        if (null != orderList) {
            var localeOrderList = getStorageVal(OrderMgr.orderListPix + type);
            if (null == localeOrderList) {
                localeOrderList = new Array();
            }
            for (index in orderList) {
                localeOrderList.push(orderList[index]);
            }
            setStorage(OrderMgr.orderListPix + type, localeOrderList);
        }
    },
    resetOrderMgr: function () {
        for (var i = OrderMgr.allOrderListType; i <= OrderMgr.pickedOrderListType; i++) {
            var key = OrderMgr.orderListPix + i;
            removeStorage(key);
            key = OrderMgr.orderListLeftNumPix + i;
            removeStorage(key);
        }
        removeStorage(OrderMgr.orderInfoCur);
    },
    wxPayOrderSucCallback: function () {
        var orderInfo = OrderMgr.getCurOrderInfo();
        if (null != orderInfo) {
            orderInfo.order_type = OrderMgr.unpickedOrderType;
            OrderMgr.setCurOrderInfo(orderInfo);
            OrderMgr.removeOrder(OrderMgr.allOrderListType, orderInfo.orderid);
            OrderMgr.removeOrder(OrderMgr.failPayOrderListType, orderInfo.orderid);
            OrderMgr.addOrder(OrderMgr.allOrderListType, orderInfo);
            OrderMgr.addOrder(OrderMgr.unpickedOrderListType, orderInfo);
        }
    },
    wxPayOrderFailedCallback: function() {
        var orderInfo = OrderMgr.getCurOrderInfo();
        if (null != orderInfo) {
        }
        var sessionId = UserInfoMgr.getSessionId();
        var wxPayFailedRequestJson = {
            "order_id": orderInfo.order_id,
            "pay_type": OrderMgr.weixinPayType,
            "order_type": OrderMgr.failPayOrderType,
            "session_id": sessionId
        };
        doAjax(wxPayFailedRequestJson, "order_fail_pay_report", function (msg) {});
    },
    getOrderTypeChangeJson: function(orderId, typeOp) {
        var sessionId = UserInfoMgr.getSessionId();
        var orderTypeChangeJson = {
            "orderid": orderId,
            "order_op_type": typeOp,
            "session_id": sessionId
        };
        return orderTypeChangeJson;
    },
    cancelOrder: function (orderInfo, cancelOrderCallback) {
        var orderTypeChangeJson = OrderMgr.getOrderTypeChangeJson(orderInfo.orderid, OrderMgr.orderCancelOp);
        doAjax(orderTypeChangeJson, "order_type_changed", function (msg) {
            if (0 == msg.result) {
                OrderMgr.removeOrder(OrderMgr.allOrderListType, orderInfo.orderid);
                OrderMgr.removeOrder(OrderMgr.failPayOrderListType, orderInfo.orderid);
            }
            if (cancelOrderCallback) {
                cancelOrderCallback();
            }
        });
    },
    removeOrder: function (orderListType, orderId) {
        var orderList = getStorageVal(OrderMgr.orderListPix + orderListType);
        if (null != orderList) {
            var bChange = false;
            for (index in orderList) {
                if (orderId == orderList[index].orderid) {
                    orderList.splice(index, 1);
                    bChange = true;
                    break;
                }
            }
            if (true == bChange) {
                setStorage(OrderMgr.orderListPix + orderListType, orderList);
            }
        }
    },
    addOrder: function (orderListType, orderInfo) {
        var orderList = getStorageVal(OrderMgr.orderListPix + orderListType);
        if (null == orderList) {
            orderList = new Array();
        }
        orderList.splice(0, 0, orderInfo);
        setStorage(OrderMgr.orderListPix + orderListType, orderList);
    },
    getOrderInfo: function(orderListType, orderId, isRemove) {
        var orderInfo = null;
        var orderList = getStorageVal(OrderMgr.orderListPix + orderListType);
        if (null != orderList && orderList != "") {
            var bChange = false;
            $.each(orderList, function (index, itemValue) {
                if (itemValue.orderid == orderId) {
                    orderInfo =itemValue;
                    if (true == isRemove) {
                        orderId.splice(index, 1);
                        bChange = true;
                    }
                }
                if (true == bChange) {
                    setStorage(OrderMgr.orderListPix + orderListType, orderList);
                }
            });
        }
        return orderInfo;
    },
    takeOrder: function (orderInfo) {
        var orderTypeChangeJson = OrderMgr.getOrderTypeChangeJson(orderInfo.orderid, OrderMgr.unpickedOrderListType);
        doAjax(orderTypeChangeJson, "order_type_changed", function (msg) {}); // 通知服务器取货，这时候并不改变订单的状态。而是有服务器的回调通知改变状态
    },
    orderTakeOffNotify: function (orderId) {
        var orderInfo = OrderMgr.getOrderInfo(OrderMgr.unpickedOrderListType, orderId, true);
        if (null != orderInfo) {
            orderInfo.order_type = OrderMgr.pickedOrderType;
            OrderMgr.addOrder(OrderMgr.pickedOrderListType, orderInfo);
        } else {
            removeStorage(OrderMgr.orderListPix + OrderMgr.unpickedOrderListType);
        }
        orderInfo = OrderMgr.getOrderInfo(OrderMgr.allOrderListType, orderId, true);
        if (null != orderInfo) {
            orderInfo.order_type = OrderMgr.pickedOrderType;
            OrderMgr.addOrder(OrderMgr.allOrderListType, orderInfo);
        } else {
            removeStorage(OrderMgr.orderListPix + OrderMgr.allOrderListType);
        }

        OrderMgr.changeOrderShow(orderInfo);
    },
    changeOrderShow: function (orderInfo) {
        if (null != orderInfo) {
            if (null != OrderMgr.orderChangeNotifyCallback) {
                OrderMgr.orderChangeNotifyCallback(orderInfo);
            }
        }
    },
    codPay: function(orderPayInfo) {
        var orderId = orderPayInfo.order_id;
        var sessionId = UserInfoMgr.getSessionId();
        var orderPayRequestJson = {
            "order_id": orderId,
            "session_id": sessionId
        };
        doAjax(orderPayRequestJson, "cod_order_report", function (msg) {
            if (msg.result == "0") {
                var orderInfo = msg.order_info;
                OrderMgr.getOrderInfo(OrderMgr.failPayOrderListType, orderInfo.orderid, true);
                OrderMgr.addOrder(OrderMgr.unpickedOrderListType, orderInfo);
                OrderMgr.getOrderInfo(OrderMgr.allOrderListType, orderInfo.orderid, true);
                OrderMgr.addOrder(OrderMgr.allOrderListType, orderInfo);
                
                OrderMgr.changeOrderShow(orderInfo);
            }
        });
    },
    orderPayInfo: function (payType, orderPayInfo) {//这里是订单真实支付逻辑，判断是微信支付还是货到付款支付
        if (OrderMgr.codPaytType == payType) {
            OrderMgr.codPay(orderPayInfo);
        } else if (OrderMgr.weixinPayType == payType) {
            //这里是微信支付的内容
        }
    },
    orderRepayHandle: function (orderInfo, validOrderCallback, invalidOrderCallback) {
        if (orderInfo != null) {
            var sessionId = UserInfoMgr.getSessionId();
            var orderRePayRequestJson = {
                "pay_type": orderInfo.pay_type,
                "order_id": orderInfo.orderid,
                "session_id": sessionId
            };
            doAjax(orderRePayRequestJson, "order_repay_handle", function (msg) {
                if (Number(msg.result) == 0) {
                    if (null != validOrderCallback) {
                        validOrderCallback(msg);
                    }
                } else {
                    if (null != invalidOrderCallback) {
                        invalidOrderCallback();
                    }
                }
            });
        }
    },
    //这个函数判断是否有未取货的商品
    isPickedOrder: function (orderInfo) {
        var isUnpickedOrder = false;
        var goodsListInfo = orderInfo.goods_info;
        for (i in goodsListInfo) {
            var goodsCodeList = goodsListInfo[i].goods_code;
            for (j in goodsCodeList) {
                if (goodsCodeList[j] != "0000000000") {
                    isUnpickedOrder = true;
                    break;
                }
            }
        }
        return isUnpickedOrder;
    },
    changeGoodsPickStatus: function (orderid, goodsId, goodsCode) {
        var orderInfo = OrderMgr.getOrderInfo(OrderMgr.unpickedOrderListType, orderid, false);
        var goodsListInfo = orderInfo.goods_info;
        for (i in goodsListInfo) {
            var goodsCodeList = goodsListInfo[i].goods_code;
            if (goodsListInfo[i].goods_info.goodsid == goodsId) {
                for (j in goodsCodeList) {
                    if (goodsCodeList[j] == goodsCode) {
                        goodsCodeList[j] = "0000000000";
                        break;
                    }
                }
            }
        }
        var isUnpickedOrder = OrderMgr.isPickedOrder(orderInfo);
        if (false == isUnpickedOrder) {
            OrderMgr.removeOrder(OrderMgr.allOrderListType, orderInfo.orderid);
            OrderMgr.removeOrder(OrderMgr.unpickedOrderListType, orderInfo.orderid);
            orderInfo.order_type = 3;
            OrderMgr.addOrder(OrderMgr.allOrderListType, orderInfo);
            OrderMgr.addOrder(OrderMgr.pickedOrderListType, orderInfo);
        }
        return orderInfo;
    },
    //akey_pick_info_notify对应的js函数
    pickGoodsNotify: function (result, msg, orderId, goodsId, goodsCode) {
        if (result == 0) {
            var orderInfo = OrderMgr.changeGoodsPickStatus(orderId, goodsId, goodsCode);
            if (OrderMgr.pickedGoodsSucNotifyCallback != null) {
                OrderMgr.pickedGoodsSucNotifyCallback(orderInfo);
            }
        } else {
            if (OrderMgr.pickedGoodsErrorNotifyCallback != null) {
                OrderMgr.pickedGoodsErrorNotifyCallback(msg);
            }
        }
    },
    getAkeyPickResult: function (akeyPickResultJson) {
        doAjax(akeyPickResultJson, "wx_get_akey_pick_result", function (msg) {
            if (Number(msg.result) == 0) {
                var orderInfo = OrderMgr.changeGoodsPickStatus(akeyPickResultJson.order_id, akeyPickResultJson.good_id, akeyPickResultJson.goods_code);
                if (OrderMgr.pickedGoodsSucNotifyCallback != null) {
                    OrderMgr.pickedGoodsSucNotifyCallback(orderInfo);
                }
            } else if (Number(msg.result) == 13) {
                setTimeout(function (e) { OrderMgr.getAkeyPickResult(e) }, 3000, akeyPickResultJson);
            } else {
                if (OrderMgr.pickedGoodsErrorNotifyCallback != null) {
                    OrderMgr.pickedGoodsErrorNotifyCallback(msg.msg);
                }
            }
        });
    },
    //order_goods_changed_notify对应的js函数
    orderGoodsChangeNotify: function (orderId, goodsId, goodsCode) {
        var orderInfo = OrderMgr.changeGoodsPickStatus(orderId, goodsId, goodsCode);
        if (OrderMgr.orderGoodsChangeNotifyCallback != null) {
            OrderMgr.orderGoodsChangeNotifyCallback(orderInfo);
        }
    },
    getTotalFee: function (orderInfo) {
        var totalFee = 0;
        for (var i = 0; i < orderInfo.goods_info.length; i++) {
            totalFee += orderInfo.goods_info[i].goods_info.price * orderInfo.goods_info[i].goods_code.length;
        }
        return totalFee;
    },
};